<template>
  <div class="home">
    <van-nav-bar
      :title="deviceInfo.devicename"
      left-arrow
      @click-left="onClickLeft"
      @click-right="onClickRight"
    >
      <template #right>
        <van-icon name="info" size="22" />
      </template>
    </van-nav-bar>
    <div class="home-content">
      <p class="home-content-area-text" v-if="camera.cameraState">摄像头</p>
      <div class="home-content-cam" v-if="camera.cameraState">
        <img class="home-content-cam-img" :src="camera.cameraIP" alt="" />
      </div>
      <p class="home-content-area-text" v-if="infoData.length !== 0">
        基本信息
      </p>
      <!-- {{this.$route.params.resData}} -->
      <div class="home-content-info">
        <li
          class="home-content-info-item"
          @click="onDevices(item, index)"
          v-for="(item, index) in infoData"
          :key="item"
        >
          <div class="home-content-info-item-title">
            <p class="home-content-info-item-title-name">{{ item.name }}</p>
          </div>
          <p class="home-content-info-item-data" :style="{ color: item.color }">
            {{
              item.specs.type === "digital"
                ? item.content === true
                  ? item.specs.high
                  : item.content === false
                  ? item.specs.low
                  : "--"
                : item.content
                ? item.content
                : "--"
            }}{{ item.specs.unit }}
          </p>
        </li>
      </div>
      <p class="home-content-area-text" v-if="controlData.length !== 0">
        设备控制
      </p>
      <div class="home-content-echarts" v-for="item in controlData" :key="item">
        <van-cell
          v-if="item.specs.type === 'digital'"
          center
          size="large"
          :label="item.content === true ? item.specs.high : item.specs.low"
          :title="item.name"
        >
          <template #right-icon>
            <van-switch
              v-model="item.content"
              :loading="item.load"
              @change="onControlChange(item)"
              size="25"
            />
          </template>
        </van-cell>
        <van-cell
          v-if="item.specs.type === 'analog'"
          center
          size="large"
          :label="item.content + item.specs.unit"
          :title="item.name"
        >
          <template #right-icon>
            <van-stepper
              v-model="item.content"
              :step="item.specs.step"
              :min="Number(item.specs.min)"
              :max="Number(item.specs.max)"
              @plus="onSetChange(item)"
              @minus="onSetChange(item)"
            />
          </template>
        </van-cell>
      </div>
      <p
        class="home-content-area-text"
        @click="chart"
        v-if="chartData.length !== 0"
      >
        图表数据
      </p>
      <div class="home-content-myecharts" v-for="item in chartData" :key="item">
        <div
          :id="item.identifier"
          style="margin: 10px; width: auto; height: 30vh"
        ></div>
      </div>
    </div>
  </div>
</template>

<script>
import { Toast } from "vant";
import * as echarts from "echarts";
var send = {};
export default {
  name: "interface_page_index",
  components: {},
  created: function () {},
  mounted: function () {
    this.initInterface();
    setTimeout(() => {
      this.myEcharts();
    }, 10);
  },
  data: function () {
    return {
      paramsRes: {},
      deviceInfo: {},
      charts: [],
      tableData: [],
      iData: [],
      cresData: [],
      camera: "",
    };
  },
  watch: {
    cresData: {
      handler(newVal, oldVal) {
        console.log(newVal);
        for (let i = 0; i < newVal.length; i++) {
          console.log(newVal[i]);
          this.chart[i].setOption({
            xAxis: {
              data: newVal[i].date,
            },
            series: [
              {
                data: newVal[i].data,
              },
            ],
          });
        }
      },
      deep: true,
    },
  },
  computed: {
    infoData: function () {
      return this.iData.filter(function (iDatas) {
        return iDatas.accessMode === "r"; //返回isShow=true的项，添加到activeUsers数组
      });
    },
    controlData: function () {
      return this.iData.filter(function (iDatas) {
        return iDatas.accessMode === "rw"; //返回isShow=true的项，添加到activeUsers数组
      });
    },
    chartData: function () {
      return this.iData.filter(function (iDatas) {
        return iDatas.view.chart; //返回isShow=true的项，添加到activeUsers数组
      });
    },
  },
  activated: function () {
    // 在 keepAlive 被激活的时候，调用指定加载页面组件的方法
    console.log("indexkeepalive");
    console.log(this.paramsRes);
    //this.myEcharts();
    this.camera = JSON.parse(localStorage.getItem(this.deviceInfo.id));
    if (this.camera === null) {
      this.camera = {
        cameraState: false,
        cameraIP: "",
      };
      localStorage.setItem(this.deviceInfo.id, JSON.stringify(this.camera));
    }
  },
  methods: {
    chart() {
      console.log(this.chartData);
    },
    initInterface() {
      this.paramsRes = this.$route.params;
      this.iData = this.paramsRes.resData;
      this.cresData = this.paramsRes.cData;
      this.deviceInfo = this.paramsRes.deviceInfo;
      var id = this.deviceInfo.id;
      this.camera = JSON.parse(localStorage.getItem(id));
      console.log(this.camera);
    },

    myEcharts() {
      // 基于准备好的dom，初始化echarts实例
      this.chart = [];
      for (let i = 0; i < this.chartData.length; i++) {
        var info = this.chartData[i];
        this.chart.push(echarts.init(document.getElementById(info.identifier)));
        this.chart[i].setOption({
          tooltip: {
            trigger: "axis",
            position: function (pt) {
              return [pt[0], "10%"];
            },
          },
          title: {
            left: "center",
            text: info.name,
          },
          xAxis: {
            type: "category",
            boundaryGap: false,
            data: this.$route.params.cData[0].date,
          },
          yAxis: {
            type: "value",
            boundaryGap: [0, "100%"],
            min: info.specs.min,
            max: info.specs.max,
          },
          series: [
            {
              name: info.name,
              type: "line",
              symbol: "none",
              sampling: "lttb",
              itemStyle: {
                color: "rgb(255, 70, 131)",
              },
              areaStyle: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    offset: 0,
                    color: "rgb(255, 158, 68)",
                  },
                  {
                    offset: 1,
                    color: "rgb(255, 70, 131)",
                  },
                ]),
              },
              data: this.$route.params.cData[0].data,
            },
          ],
        });
      }
      // this.speedChart = echarts.init(document.getElementById("RelaySet"));
      // this.speedChart
    },
    onClickLeft() {
      this.$router.back(-1);
    },
    onClickRight() {
      this.$router.push({
        name: "interface_page_info",
        params: {
          // resData: this.paramsRes.resData,
          // cData: this.paramsRes.cData,
          deviceInfo: this.paramsRes.deviceInfo,
          routerType: "push",
        },
      });
    },
    onControlChange(item) {
      navigator.vibrate(100);
      console.log(item);
      this.$set(item, "load", true);
      //this.$mqttcontrol.digitalWrite(info,this.sub)
      var send = {};
      send[item.identifier] = item.content === true ? "1" : "0";
      this.$mqtt.publish(this.deviceInfo.mqttSub, send);
    },

    onSetChange(item) {
      this.$set(item, "load", true);
      clearTimeout(this.timer);
      this.timer = setTimeout(() => {
        console.log(item.content);
        var value = item.content;
        Toast.loading({ forbidClick: true });
        var send = {};
        send[item.identifier] = value;
        console.log(send);
        this.$mqtt.publish(this.deviceInfo.mqttSub, send);
      }, 500);
    },
  },
};
</script>

<style lang="scss" scoped>
.home {
  width: 100%;
  height: 100%;
  overflow: auto;
  display: flex;
  flex-flow: column;
  position: absolute;
  &-content {
    height: 100%;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: left;
    justify-content: left;
    transition: 0.3s;
    overflow-x: hidden;
    &-cam {
      -webkit-animation: xhe 0.4s ease;
      //position: relative;
      margin: 0 20px 0 20px;
      padding: 10px 10px;
      border-radius: 20px 20px 20px 20px;
      box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.3);
      background-color: rgba(255, 255, 255, 0.85);
      backdrop-filter: blur(20px) saturate(180%);
      background-attachment: fixed;
      display: flex;
      justify-content: center;
      align-items: center;
      width: auto;
      height: 70vw;
      &-img {
        border-radius: 20px 20px 20px 20px;
        width: 100%;
        height: auto;
        min-height: 70vw;
      }
    }
    &-myecharts {
      -webkit-animation: xhe 0.4s ease;
      text-align: center;
      margin: 0 16px 15px;
      border-radius: 20px 20px 20px 20px;
      // box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.3);
      width: auto;
      //height: 40vh;
      display: flex;
      position: relative;
      flex-direction: column;
      justify-content: left;
      background-color: rgba(255, 255, 255, 0.85);
      backdrop-filter: blur(20px) saturate(180%);
      background-attachment: fixed;
    }
    &-echarts {
      -webkit-animation: xhe 0.4s ease;
      margin: 0 16px 0px;
      width: auto;
      display: flex;
      position: relative;
      flex-direction: column;
      justify-content: left;
      background-attachment: fixed;
    }
    &-control {
      border-radius: 20px;
      margin-top: 0px;
      margin-left: 20px;
      margin-right: 20px;
      margin-bottom: 20px;
    }
    &-area {
      &-text {
        -webkit-animation: hue 0.6s ease;
        //transition: all 0.6s ease;
        //transform: translateY(100px);
        //transition: .5s;
        margin-left: 20px;
        font-size: 5vw;
        color: white;
        font-weight: 900;
      }
    }
    &-text {
      margin: px2rem(20);
      font-size: px2rem(30);
      color: white;
      font-weight: 600;
      margin-bottom: px2rem(30);
    }
    &-shebei {
      margin-left: px2rem(20);
      margin-bottom: px2rem(5);
      font-size: px2rem(14);
      color: white;
      font-weight: 400;
    }
    &-info {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      padding: 0 16px 0 16px;

      &-item {
        -webkit-animation: xhe 0.4s ease;
        height: auto;
        // transition: 0.4s;
        margin: 5px;
        text-align: center;
        padding: 5px;
        border-radius: 20px 20px 20px 20px;
        // box-shadow: 0 0 16px 0 rgba(0, 0, 0, 0.3);
        width: calc(50% - 20px);
        // height: calc(50% - 10px);
        display: flex;
        position: relative;
        flex-direction: column;
        justify-content: left;
        background-color: rgba(255, 255, 255, 0.85);
        backdrop-filter: blur(20px) saturate(180%);
        background-attachment: fixed;
        &-title {
          display: flex;
          flex-direction: row;
          align-items: center;

          &-name {
            margin: auto;
            transition: 0.4s;
            text-align: left;
            color: black;
            //margin-top: 0;
            font-size: 4vw;
          }
        }

        &-data {
          transition: 0.4s;
          // text-align: right;
          color: black;
          margin: auto;
          font-size: 9vw;
          font-weight: 500;
        }
      }
    }
  }
}
.custom-button {
  width: auto;
  padding: 5px 10px;
  color: #fff;
  font-size: 4vw;
  line-height: 5vw;
  text-align: center;
  background-color: #0aadee;
  border-radius: 20px;
}
.van-cell {
  margin: 0 0 10px 0;
  width: auto;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(20px) saturate(180%);
}
@-webkit-keyframes hue {
  from {
    transform: translateX(100px);
  }
  to {
    transform: translateY(0);
  }
}
@-webkit-keyframes xhe {
  from {
    transform: scale(0);
    // transform: translateX(50vw);
  }
  to {
    transform: scale(1);
    // transform: translateY(0);
  }
}
</style>
